package com.fh.rolemenu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.rolemenu.entity.UmsRoleMenu;
import com.fh.rolemenu.mapper.UmsRoleMenuMapper;
import com.fh.rolemenu.service.IUmsRoleMenuService;
import com.fh.search.MenuToUser;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-17
 */
@Service
public class UmsRoleMenuServiceImpl extends ServiceImpl<UmsRoleMenuMapper, UmsRoleMenu> implements IUmsRoleMenuService {

    @Override
    public void SaveMenuToUser(MenuToUser menuToUser) {
        QueryWrapper<UmsRoleMenu> queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", menuToUser.getUserId());
        remove(queryWrapper);
        if (CollectionUtils.isNotEmpty(menuToUser.getMenuIdList())) {
            List<UmsRoleMenu> list = new ArrayList<>();
            menuToUser.getMenuIdList().forEach(menuId -> {
                UmsRoleMenu usertomenu = new UmsRoleMenu();
                usertomenu.setRoleId(menuToUser.getUserId());
                usertomenu.setMenuId(menuId);
                list.add(usertomenu);
            });
            saveBatch(list);
        }
    }
}
